package com.pshare.tool;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.apache.log4j.Logger;

/**
 * 邮箱MX工具
 */
public class NSTool {

	private static final Logger logger = Logger.getLogger(NSTool.class);

	public static List<String> getMxRecord(String hostName) {
		List<String> mxList = new ArrayList<String>();
		try {
			Hashtable env = new Hashtable();
			env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
			DirContext ictx = new InitialDirContext(env);
			Attributes attrs = ictx.getAttributes(hostName,
					new String[] { "MX" });
			Attribute attr = attrs.get("MX");
			for (int i = 0; i < attr.size(); i++) {
				String mxstr = (String) attr.get(i);
				mxList.add(mxstr);
			}
		} catch (NamingException ne) {
			logger.error(ne.getMessage(), ne);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

		return mxList;
	}

}
